package com.ruoyi.job.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.job.service.ISysJobLogService;
import com.ruoyi.system.api.domain.SysDictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.job.domain.SysJobLog;
import com.ruoyi.job.mapper.SysJobLogMapper;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author ruoyi
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {
    @Autowired
    private SysJobLogMapper jobLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        return jobLogMapper.selectJobLogList(jobLog);
    }

    /**
     * 获取quartz调度器日志的计划任务（分页）
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public TableDataInfo<SysJobLog> selectJobLogPage(SysJobLog jobLog, PageQuery pageQuery) {
        LambdaQueryWrapper<SysJobLog> lqw = buildQueryWrapper(jobLog);
        Page<SysJobLog> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    private LambdaQueryWrapper<SysJobLog> buildQueryWrapper(SysJobLog jobLog) {
        Map<String, Object> params = jobLog.getParams();
        LambdaQueryWrapper<SysJobLog> lqw = Wrappers.lambdaQuery();
        lqw.like(!StringUtils.isEmpty(jobLog.getJobName()), SysJobLog::getJobName, jobLog.getJobName())
                .eq(!StringUtils.isNull(jobLog.getJobGroup()), SysJobLog::getJobGroup, jobLog.getJobGroup())
                .eq(!StringUtils.isNull(jobLog.getStatus()), SysJobLog::getStatus, jobLog.getStatus())
                .like(!StringUtils.isEmpty(jobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, jobLog.getInvokeTarget())
                .ge(params.get("beginTime") != null, SysJobLog::getCreateTime, params.get("beginTime"))
                .le(params.get("endTime") != null, SysJobLog::getCreateTime, params.get("endTime"));
        return lqw;
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return jobLogMapper.selectJobLogById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        jobLogMapper.insertJobLog(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return jobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return jobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        jobLogMapper.cleanJobLog();
    }
}
